package com.leo.erp.web.organization;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.leo.erp.dto.OrganizationExecution;
import com.leo.erp.entity.Organization;
import com.leo.erp.entity.User;
import com.leo.erp.enums.OrganizationStateEnum;
import com.leo.erp.service.OrganizationService;
import com.leo.erp.util.HttpServletRequestUtil;
import com.leo.erp.util.TreeNode;

@Controller
@RequestMapping(value = "/organization")
public class OrganizationManagementController {
	@Autowired
	private OrganizationService organizationService;
	
	@RequestMapping(value = "/batchDeleteOrganization")
	@ResponseBody
	public Map<String, Object> batchDeleteOrganization(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		String ids = HttpServletRequestUtil.getString(request, "ids");
		OrganizationExecution oe =organizationService.batchDeleteOrganizationByIds(ids);
		if(oe.getState() == OrganizationStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "删除机构信息失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/editOrganization")
	@ResponseBody
	public Map<String, Object> editOrganization(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		Organization organization = JSON.parseObject(beanJson, Organization.class);
		
		OrganizationExecution oe = organizationService.editOrganization(organization);
		if(oe.getState() == OrganizationStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "修改机构信息失败");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/findById")
	@ResponseBody
	public Map<String, Object> findById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Map<String, Object> map= new HashMap<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取前台传过来的值
		int orgId = HttpServletRequestUtil.getInt(request, "id");
		OrganizationExecution oe = organizationService.findById(orgId);
		if(oe.getState() == OrganizationStateEnum.SUCCESS.getState()) {
			map.put("id", oe.getOrganization().getOrgId());
			map.put("orgFullName", oe.getOrganization().getOrgFullName());
			map.put("orgAbr", oe.getOrganization().getOrgAbr());
			map.put("orgParentNo", oe.getOrganization().getOrgParentNo());
			
			OrganizationExecution oeParent = organizationService.findByOrgNo(oe.getOrganization().getOrgParentNo());
			if(oeParent.getState() == OrganizationStateEnum.SUCCESS.getState()) {
				//父级机构名称显示简称
				map.put("orgParentName", oeParent.getOrganization().getOrgAbr());
			}
			map.put("orgTpcd", oe.getOrganization().getOrgTpcd());
			map.put("orgStcd", oe.getOrganization().getOrgStcd());
			map.put("orgNo", oe.getOrganization().getOrgNo());
			map.put("sort", oe.getOrganization().getSort());
			/**
			 * 判断时间不为空再做转化
			 */
			if(oe.getOrganization().getOrgCreateTime() != null) {
				map.put("orgCreateTime", sdf.format(oe.getOrganization().getOrgCreateTime()));
			}
			if(oe.getOrganization().getOrgStopTime() != null) {
				map.put("orgStopTime", sdf.format(oe.getOrganization().getOrgStopTime()));
			}
			map.put("remark", oe.getOrganization().getRemark());
			modelMap.put("success", true);
			modelMap.put("data", map);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}
		
		return modelMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/addOrganization")
	public Map<String, Object> addOrganization(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		String beanJson = HttpServletRequestUtil.getString(request, "info");
		Organization organization = JSON.parseObject(beanJson, Organization.class);
		//session
		User user = (User)request.getSession().getAttribute("user");
		organization.setTenantId(user.getUserId());
		organization.setCreator(user.getUserId());
		//调用插入数据方法
		OrganizationExecution oe = organizationService.addOrganization(organization);
		if(oe.getState() == OrganizationStateEnum.SUCCESS.getState()) {
			modelMap.put("success", true);
		}else {
			if(oe.getState() == OrganizationStateEnum.ORGNO_EXISTS.getState()) {
				modelMap.put("success", false);
				modelMap.put("msg", OrganizationStateEnum.ORGNO_EXISTS.getStateInfo());
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "添加机构异常");
			}
		}
		
		return modelMap;
	}
	
	/**
	 * 获取机构树
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOrganizationTree")
	@ResponseBody
	public JSONArray getOrganizationTree(HttpServletRequest request){
		int currentId = HttpServletRequestUtil.getInt(request, "id");
		JSONArray arr=new JSONArray();
		//session
		User user = (User) request.getSession().getAttribute("user");
		int tenantId = user.getUserId();
		
		List<TreeNode> organizationTree= organizationService.getOrganizationTree(currentId, tenantId);
		if(organizationTree!=null&&organizationTree.size()>0){
	    	for(TreeNode node:organizationTree){
	        	String str=JSON.toJSONString(node);
	        	JSONObject obj=JSON.parseObject(str);
	        	arr.add(obj) ;
	    	}
		}
		return arr;
	}
	
}
